package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.wetsion.employ.server.mapper.ForumMapper;
import site.wetsion.employ.server.mapper.UserMapper;
import site.wetsion.employ.server.pojo.ForumFollow;
import site.wetsion.employ.server.pojo.ForumPost;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.util.Page;

@Service
public class ForumService {
	
	private final static Logger log = LoggerFactory.getLogger(ForumService.class);


	@Autowired
	private ForumMapper forumMapper;
	@Autowired
	private UserMapper userMapper;
	
	/** 发布一条帖子*/
	public boolean addForumPost(ForumPost post){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		post.setGmt_create(timestamp);
		post.setGmt_modified(timestamp);
		try {
			forumMapper.insertForumPost(post);
			return true;
		} catch (Exception e) {
			log.error("插入帖子失败", e);
			return false;
		}
	}
	/** 添加一条回复*/
	public boolean addForumFollow(ForumFollow follow){
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		follow.setGmt_create(timestamp);
		follow.setGmt_modified(timestamp);
		try {
			forumMapper.insertForumFollow(follow);
			return true;
		} catch (Exception e) {
			log.error("插入回复失败", e);
			return false;
		}
	}
	
	/** 分页获取帖子列表*/
	public List<ForumPost> getForumPostListByPage(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<ForumPost> list = forumMapper.getForumPostListByPage((pageNum-1)*pageSize, pageSize);
			Collections.sort(list);
			for (ForumPost forumPost : list) {
				User user = userMapper.findUserById(forumPost.getPost_starter_id());
				forumPost.setPost_starter(user);
			}
			return list;
		} catch (Exception e) {
			log.error("获取帖子列表失败", e);
			return null;
		}
	}
	
	/** 获取帖子总数*/
	public int getCountOfForumPost(){
		try {
			return forumMapper.getCountOfForumPostList();
		} catch (Exception e) {
			log.error("获取帖子总数失败", e);
			return 0;
		}
	}
	/** 获取搜索的帖子列表*/
	public List<ForumPost> getForumPostListOnSearchByPage(Page page, String keyword) {
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<ForumPost> list = forumMapper.
					getForumPostListOnSearchByPage((pageNum-1)*pageSize, pageSize, keyword);
			Collections.sort(list);
			for (ForumPost forumPost : list) {
				User user = userMapper.findUserById(forumPost.getPost_starter_id());
				forumPost.setPost_starter(user);
			}
			return list;
		} catch (Exception e) {
			log.error("获取搜索的帖子列表失败", e);
			return null;
		}
	}
	
	/** 获取搜索帖子总数*/
	public int getCountOfSearchForumPost(String keyword){
		try {
			return forumMapper.getCountOfSearchForumPost(keyword);
		} catch (Exception e) {
			log.error("获取搜索的帖子总数失败", e);
			return 0;
		}
	}
	
	/** 根据id获取帖子*/
	public ForumPost getForumPostById(BigInteger id){
		try {
			ForumPost post = forumMapper.getForumPostById(id);
			post.setPost_starter(userMapper.findUserById(post.getPost_starter_id()));
			return post;
		} catch (Exception e) {
			log.error("根据id获取帖子失败，id="+id, e);
			return null;
		}
	}
	
	/** 获取帖子下的所有回复*/
	public List<ForumFollow> getForumPostDetail(Page page, BigInteger id){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<ForumFollow> follows = forumMapper.getForumFollowByPostIdByPage((pageNum-1)*pageSize, pageSize, id);
			for (ForumFollow forumFollow : follows) {
				ForumFollow parent = forumMapper.getForumFollowById(forumFollow.getFollow_parent_id());
				forumFollow.setParent_follow(parent);
				User user = userMapper.findUserById(forumFollow.getFollow_user_id());
				forumFollow.setFollow_user(user);
				if(parent!=null){
					parent.setFollow_user(userMapper.findUserById(parent.getFollow_user_id()));
				}
			}
			return follows;
		} catch (Exception e) {
			log.error("获取帖子详情失败", e);
			return null;
		}
	}
	
	/** 获取帖子下回复数量*/
	public int getCountOfFollowOfPost(BigInteger id){
		try {
			return forumMapper.getCountOfForumFollowList(id);
		} catch (Exception e) {
			log.error("获取帖子下回复失败", e);
			return 0;
		}
	}
	
	
	/** 分页获取用户的帖子列表*/
	public List<ForumPost> getForumPostListOfUser(Page page, BigInteger userId){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<ForumPost> list = forumMapper.getForumPostListByUserId((pageNum-1)*pageSize, pageSize, userId);
			Collections.sort(list);
			for (ForumPost forumPost : list) {
				User user = userMapper.findUserById(forumPost.getPost_starter_id());
				forumPost.setPost_starter(user);
			}
			return list;
		} catch (Exception e) {
			log.error("获取用户的帖子列表失败", e);
			return null;
		}
	}
	
	/** 获取用户的帖子总数*/
	public int getPostCountOfUser(BigInteger userId){
		try {
			return forumMapper.getPostCountOfUser(userId);
		} catch (Exception e) {
			log.error("获取用户的帖子总数失败", e);
			return 0;
		}
	}
	
	/** 根据用户id分页获取该用户的所有别人给他的回帖消息*/
	public List<ForumFollow> getAllFollowMessageOfUser(
			BigInteger userId, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<ForumFollow> followList = forumMapper.getAllFollowMessageByUid((pageNum-1)*pageSize, pageSize, userId);
			for (ForumFollow forumFollow : followList) {
				ForumPost post = getForumPostById(forumFollow.getFollow_post_id());
				forumFollow.setForumPost(post);
				User user = userMapper.findUserById(forumFollow.getFollow_user_id());
				forumFollow.setFollow_user(user);
			}
			return followList;
		} catch (Exception e) {
			log.error("根据用户id获取论坛消息失败", e);
			return null;
		}
	}
	/** 根据用户id分页获取该用户的所有别人给他的回帖消息数量*/
	public int getCountOfAllFollowMessageOfUser(
			BigInteger userId){
		try {
			return forumMapper.getCountOfAllFollowMessageByUid(userId);
		} catch (Exception e) {
			log.error("根据用户id获取论坛消息数量失败", e);
			return 0;
		}
	}
	
	/** 获取未查看的消息数*/
	public int getUnLookMessageCount(BigInteger userId) {
		try {
			return forumMapper.getCountOfUnLookFollowMessageByUid(userId);
		} catch (Exception e) {
			return 0;
		}
	}
	
	/** 设置消息已读*/
	public boolean setLookMessageByUid(BigInteger userId){
		try {
			forumMapper.setLookOfMessageByUid(userId);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/** 根据id删帖*/
	public boolean deleteForumPost(BigInteger id){
		try {
			forumMapper.deleteForumPostById(id);
			return true;
		} catch (Exception e) {
			log.error("删除帖子失败", e);
			return false;
		}
	}
	
}
